﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BonjourHotel.Core.Utils;
using BonjourHotel.Core.Localization;
using BonjourHotel.Core.Hotels;
using BonjourHotel.Core.Rooms;
using BonjourHotel.Core.Floors;
using BonjourHotel.Model;

namespace BonjourHotel.Application.Controls.Rooms
{
    public partial class RoomCreation : System.Web.UI.UserControl
    {
        /// <summary>
        /// id used to edit an existing room
        /// </summary>
        private int idRoom
        {
            get
            {
                return CoreUtils.QueryStringInt("idRoom", 0);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ApplyLocalization();

                if (idRoom != 0)
                {
                    BindData();
                }
            }
        }

        private void ApplyLocalization()
        {
            lblRoomName.Text = LocaleManager.GetLocaleString("controls.room.creation.RoomName");
            lblRoomNumber.Text = LocaleManager.GetLocaleString("controls.room.creation.RoomNumber");
            lblHotel.Text = LocaleManager.GetLocaleString("controls.room.creation.HotelReference");
            lblFloorType.Text = LocaleManager.GetLocaleString("controls.room.creation.FloorType");
            lblRoomType.Text = LocaleManager.GetLocaleString("controls.room.creation.RoomType");
            lblMaxBed.Text = LocaleManager.GetLocaleString("controls.room.creation.MaxBed");
            lblMinAdultCount.Text = LocaleManager.GetLocaleString("controls.room.creation.MinAdultCount");
            lblMaxAdultCount.Text = LocaleManager.GetLocaleString("controls.room.creation.MaxAdultCount");
            lblMinChildrenCount.Text = LocaleManager.GetLocaleString("controls.room.creation.MinChildrenCount");
            lblMaxChildrenCount.Text = LocaleManager.GetLocaleString("controls.room.creation.MaxChildrenCount");
            lblNotes.Text = LocaleManager.GetLocaleString("controls.room.creation.Notes");
            btnSave.Text = LocaleManager.GetLocaleString("controls.common.save");
            ddHotel.Items.Add(new ListItem(LocaleManager.GetLocaleString("controls.common.initialDropdownItem"), "0"));
            ddRoomType.Items.Add(new ListItem(LocaleManager.GetLocaleString("controls.common.initialDropdownItem"), "0"));
            ddFloorType.Items.Add(new ListItem(LocaleManager.GetLocaleString("controls.common.initialDropdownItem"), "0"));

            //Populate hotels combo
            HotelFacade hotelFacade = new HotelFacade();
            ddHotel.DataSource = hotelFacade.GetAllHotels();
            ddHotel.DataTextField = "Name";
            ddHotel.DataValueField = "idHotel";
            ddHotel.DataBind();

            //Populate room types combo
            RoomTypeFacade roomTypes = new RoomTypeFacade();
            ddRoomType.DataSource = roomTypes.GetAllTypes();
            ddRoomType.DataTextField = "Description";
            ddRoomType.DataValueField = "idRoomType";
            ddRoomType.DataBind();

            //Populate floor type combo
            FloorTypeFacade floorTypes = new FloorTypeFacade();
            ddFloorType.DataSource = floorTypes.GetAllTypes();
            ddFloorType.DataTextField = "Description";
            ddFloorType.DataValueField = "idFloorType";
            ddFloorType.DataBind();
        }

        private void BindData()
        {
            throw new NotImplementedException();
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                RoomFacade roomFacade = new RoomFacade();
                Room room = null;

                if (idRoom != 0)
                    room = roomFacade.GetRoom(idRoom);
                else
                {
                    room = new Room();
                    roomFacade.Add(room);
                }

                room.Name = tbRoomName.Text;
                room.RoomNumber = tbRoomNumber.Text;

                if (ddHotel.SelectedValue != "0")
                {
                    int idHotel = Int32.Parse(ddHotel.SelectedValue);
                    room.HotelReference.EntityKey = new System.Data.EntityKey("BonjourHotelEntities.HotelSet", "idHotel", idHotel);
                }
                else
                {
                    room.Hotel = null;
                }

                if (ddFloorType.SelectedValue != "0")
                {
                    int idFloorType = Int32.Parse(ddFloorType.SelectedValue);
                    room.FloorReference.EntityKey = new System.Data.EntityKey("BonjourHotelEntities.FloorTypes", "idFloorType", idFloorType);
                }
                else
                {
                    room.Floor = null;
                }

                if (ddRoomType.SelectedValue != "0")
                {
                    int idRoomType = Int32.Parse(ddRoomType.SelectedValue);
                    room.RoomTypeReference.EntityKey = new System.Data.EntityKey("BonjourHotelEntities.RoomTypes", "idRoomType", idRoomType);
                }
                else
                {
                    room.RoomType = null;
                }

                if (!String.IsNullOrEmpty(tbMaxBed.Text))
                {
                    int value = 0;
                    bool success = Int32.TryParse(tbMaxBed.Text, out value);
                    if (success) room.MaxBed = value;
                }

                if (!String.IsNullOrEmpty(tbMinAdultCount.Text))
                {
                    int value = 0;
                    bool success = Int32.TryParse(tbMinAdultCount.Text, out value);
                    if (success) room.MinAdultCount = value;
                }

                if (!String.IsNullOrEmpty(tbMaxAdultCount.Text))
                {
                    int value = 0;
                    bool success = Int32.TryParse(tbMaxAdultCount.Text, out value);
                    if (success) room.MaxAdultCount = value;
                }

                if (!String.IsNullOrEmpty(tbMinChildrenCount.Text))
                {
                    int value = 0;
                    bool success = Int32.TryParse(tbMinChildrenCount.Text, out value);
                    if (success) room.MinChildrenCount = value;
                }

                if (!String.IsNullOrEmpty(tbMaxChildrenCount.Text))
                {
                    int value = 0;
                    bool success = Int32.TryParse(tbMaxChildrenCount.Text, out value);
                    if (success) room.MaxChildrenCount = value;
                }

                room.Notes = tbNotes.Text;
                
                roomFacade.SaveAllObjectChanges();
            }
        }
    }
}